import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


public class sword extends Actor
{
    int rot=0,x=-24,a=1,counter=0;
    boolean stuck=false,falling=false;
    Color alpha = new Color(0,0,0,0);
 
    public sword(boolean right){
        if (right){
            x=24;
            a=-1;
        }

    }
    
    protected void addedToWorld(World world)
    {
        setImage(((it) getWorld()).getSword());  
    } 
    
    
    
    public void act() 
    {    
        
        
        rot=rot+15;
        if(!stuck){
            if(rot%150==15)Greenfoot.playSound("swordWhip.wav");
            setLocation(getX()+(int)(x/3),getY());
            if(Math.abs(x)!=25)x=x+a;
            setRotation(rot);
        }
        if(falling && ((it) getWorld()).ok(getX(),getY()+4))
            setLocation(getX(),getY()+2);
        
        if(!stuck && !((it) getWorld()).ok(getX(),getY()) || getX()>598){
            stuck=true;
            Greenfoot.playSound("swordStick.wav");
            if(x<0)setRotation(-90);
            else setRotation(90);
            counter=200;
            while(!((it) getWorld()).ok(getX(),getY())){
                if(x<0)setLocation(getX()+1,getY());
                if(x>0)setLocation(getX()-1,getY());
            }
            if(x<0)setLocation(getX()+5,getY());
            if(x>0)setLocation(getX()-5,getY());
        }
        
        Enemy it1 = (Enemy) getOneIntersectingObject(Enemy.class);
        if(!stuck && it1!=null){
            it1.hurt(2);
        }
        
        if(counter>0)counter--;
        if(counter==1)falling=true;
        Actor it = getOneIntersectingObject(Zony.class);
        if(it!=null || getY()>390 || ((it)getWorld()).kill(getX(),getY())){
            ((it) getWorld()).caught();
            ((it) getWorld()).removeObject(this);
        }
        
    }    
    
}
